<?php require_once ADMINVIEWPATH.'admin_header.php'; ?>
<?php require_once ADMINVIEWPATH.'admin_menu.php'; ?>

<div class="page-content">
    <!-- Page Breadcrumb -->
    <div class="page-breadcrumbs">
        <ul class="breadcrumb">
            <li>
                <i class="fa fa-home"></i>
                <a href="#">首页</a>
            </li>
            <li class="">
                <a href="/luckydog/company/index">消息管理</a>
            </li>
            <li class="active">
                <a href="#">消息发送</a>
            </li>
        </ul>
    </div>
    <!-- Page Body -->
    <div class="page-body">
       <form class="form-horizontal" role="form">
            <div class="row">
                    <div class="col-lg-12">
                        <div class="widget">
                            <div class="widget-header bordered-bottom bordered-blue">
                                <span class="widget-caption">添加器材</span>
                            </div>
                            <div class="widget-body">
                                <div id="horizontal-form">
                                    <table class="table table-bordered">
                                      <tbody>
                                        <tr>
                                            <td style="width: 100px;">收件用户</td>
                                            <td>
                                            <div style="float:left;">
                                                <select id="tosendtype"   style="width: 120px;">
                                                    <option  value="0">--发送对象--</option>
                                                    <option <?php if($draft_byid['tosendtype']=="all"):?>selected="selected" <?php endif;?>  value="all">发送全员</option>
                                                   <!--  <option <?php if($draft_byid['tosendtype']=="department"):?>selected="selected" <?php endif;?> value="department">发送部门</option> -->
                                                    <option <?php if($draft_byid['tosendtype']=="usernames"):?>selected="selected" <?php endif;?>  value="usernames">发送个人</option>
                                                </select>
                                            </div>
                                            <div style="float:left;margin-left: 20px;">
                                                <select id="department"   style="width: 220px;<?php if($draft_byid['tosendtype']!='department' && $draft_byid['tosendtype']!='all'):?>display: none;<?php endif;?>">
                                                    <option  value="0">请选择部门</option>
                                                    <?php if($departmentList):?>
                                                    <?php foreach ($departmentList as $key => $value):?>
                                                    <option <?php if($draft_byid['to_sendid']==$value['id']):?>selected="selected" <?php endif;?> value="<?=$value['id']?>"><?=$value['name']?></option>
                                                    <?php endforeach;?>
                                                    <?php endif;?>
                                                </select>
                                                <textarea id="usernames" style="width: 300px; <?php if($draft_byid['tosendtype']!='usernames' && $draft_byid['tosendtype']!='all'):?>display: none;<?php endif;?>"></textarea>
                                            </div>
                                            </td>
                                        </tr>
                                        <tr>
                                          <td>主题</td>
                                          <td>
                                            <input type="text" style="width: 98%;" name='subject' id='subject' placeholder="请输入发件主题" value="<?= $draft_byid['subject'];?>" />
                                          </td>
                                        </tr>
                                        <tr>
                                          <td>正文</td>
                                          <td><textarea id="contents" placeholder="请输入用户名，以英文;号分隔" name="contents" style="width:99%; height:400px;"><?= $draft_byid['content'];?></textarea></td>
                                        </tr>
                                        <tr>
                                          <td colspan ="2">
                                               <span class="btn btn-default" style="float:left;margin-left:130px;background-color:#28b779;"  onclick="J_send_email(0,'<?= empty($draftid) ? 0 : $draftid ?>')">发&nbsp;送</span>
                                                <span class="btn btn-default" style="float:left;margin-left:10px;" onclick="J_send_email(1,'<?= empty($draftid) ? 0 : $draftid ?>')">存草稿</span>
                                          </td>
                                        </tr>
                                      </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
        </form>
    </div>
</div>
<!-- /Page Body -->
</div>
<script src="<?= STATICURL; ?>kindeditor/kindeditor.js"></script>
<?php require_once ADMINVIEWPATH.'admin_footer.php'; ?>
<script type="text/javascript">
KindEditor.ready(function(K) {
        window.editor = K.create('#contents');
        filterMode: true;
});
$("td").css("vertical-align","middle");
$("#tosendtype").change(function(){
  var tosendtype = $(this).val();
  if(tosendtype == "all"){
      $("#department").css("display","none");
      $("#usernames").css("display","none");
  }else if(tosendtype == "department"){
      $("#department").css("display","block");
      $("#usernames").css("display","none");
  }else if(tosendtype == "usernames"){
      $("#department").css("display","none");
      $("#usernames").css("display","block");
  }else{

  }
});
function J_send_email(type,draftid){
      editor.sync();
	    var subject = $("#subject").val();
	    var content = $("#contents").val();
      var tosendtype = $("#tosendtype").val();
      var departmentid = '';
      var usernames = '';

      if(tosendtype == "all"){
          usernames = '';
          departmentid = '';
      }else if(tosendtype == "department"){
          departmentid = $("#department").val();
          usernames = '';
      }else if(tosendtype == "usernames"){
          departmentid = '';
          usernames = $("#usernames").val();
      }else{
          alert("请选择发送对象");return false;
      }
      if(type != "1")
      {
          if(departmentid == "0")
          {
            alert("请选择收件用户");
            return false;
          }
          if(subject == "")
          {
            alert("请填写发件主题");
            return false;
          }
          if(content == "")
          {
            alert("请填写发件内容");
            return false;
          }
      }
      if(departmentid == "0" && subject == "" && content == "")
      {
            alert("没有进行任何填写，不能存为草稿");
            return false;
      }
      if(type == 1)
      {
        var msg = "确定要存入草稿箱吗？";
      }else{
        var msg = "确定要发送吗？";
      }
      if (confirm(msg) == true) 
      {
          $.post('<?= BASEURL; ?>luckydog/emailsender/send_email', {'tosendtype':tosendtype,'usernames':usernames,'subject':subject, 'content':content,'departmentid':departmentid,'type':type, 'draftid':draftid}, function(res) {
          if (res == '1') 
          {
              alert('发送成功');
              window.location.href="<?= BASEURL; ?>luckydog/emailsender";
          }
          else if(res == '2')
          {
              alert('保存草稿成功');
              window.location.href="<?= BASEURL; ?>luckydog/emailsender";
          }
          else
          {
              alert(res);
          }
          });
      }
}

</script>